package com.zerosoft.boilerpanel.component;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class PilotLamp extends VBox {
    @FXML
    public Label pilotLampLabel;
    @FXML
    private ImageView pilotLamp;

    public PilotLamp() {
        super();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("pilot-lamp.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

//        this.getChildren().add(this.pilotLamp);
    }

//    public ObjectProperty imageProperty() {
//        return pilotLamp.imageProperty();
//    }

    public StringProperty textProperty() {
        return pilotLampLabel.textProperty();
    }

    public void setText(String text) {
        pilotLampLabel.setText(text);
    }
    public void setImage(String image) {
        pilotLamp.setImage(new Image(getClass().getResourceAsStream(image)));
    }

    public String getText(){
        return pilotLampLabel.getText();
    }

    public String getImage(){
        return pilotLamp.getImage().getUrl();
    }

    public void blink(){
        Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
//        opacity = pilotLamp.getOpacity();
        KeyFrame keyFrame = new KeyFrame(javafx.util.Duration.seconds(0.2),
                event -> {
                    pilotLamp.setOpacity(1 - pilotLamp.getOpacity());
                }
        );

        timeline.getKeyFrames().add(keyFrame);
        timeline.play();




//        pilotLamp.setVisible(true);
//        new Thread(() -> {
//            try {
//                Thread.sleep(500);
//                pilotLamp.setVisible(false);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }).start();
    }

}
